<template>
  <div class="NewsLogin">
    <div class="login" v-if="!showConfig">
      <div class="main">
        <div class="title">请先登录</div>

        <div class="item">
          <span>密码:</span>
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </div>
        <div class="item">
          <el-button
            class="btn"
            type="primary"
            native-type="submit"
            @click="submitForm('ruleForm')"
          >确定</el-button>
        </div>
      </div>
    </div>
    <NewsConfig v-if="showConfig" />
  </div>
</template>

<script>
import NewsConfig from "./NewsConfig.vue";
export default {
  components: {
    NewsConfig
  },
  name: "NewsLogin",
  data() {
    return {
      showConfig: false,
      ruleForm: {
        pass: ""
      }
    };
  },
  mounted() {
    this.initial();
  },
  beforeDestroy() {
    // console.log('我他妈销毁啦');
    //移除回车事件
    document.removeEventListener("keydown", this.keyDownEvt);
  },
  methods: {
    async initial() {
      //已经登录过了
      if (sessionStorage.getItem("newsConfigPass") == "true") {
        this.showConfig = true;
      } else {
        //未登录
        document.addEventListener("keydown", this.keyDownEvt);
      }
    },
    keyDownEvt(e) {
      if (e.keyCode == 13 || e.key == "Enter") {
        //按了回车
        this.submitForm();
      }
    },
    submitForm() {
      if (this.ruleForm.pass == "321196") {
        sessionStorage.setItem("newsConfigPass", "true");
        this.$message.success("登录成功");
        //移除回车事件
        document.removeEventListener("keydown", this.keyDownEvt);
        setTimeout(() => {
          this.showConfig = true;
        }, 1000);
      } else {
        this.$message.error("密码错误");
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.NewsLogin {
  height: 100vh;
  width: 100vw;
  .login {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    .main {
      width: 400px;
      .title {
        font-size: 25px;
        text-align: center;
        padding: 20px;
      }
      .item {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 30px;
        span {
          width: 60px;
        }
        .btn {
          width: 200px;
        }
      }
      // height: 400px;
    }
  }
}
</style>
